import myselfService from '../../api/myself-api'

const app = getApp()
Page({ 
  data:{
    nickName:'',
    dialogShow:false,
    avatarUrl:'../../static/images/default_user.png',
  },
  register:function(e){
    if(!app.globalData.userInfo){
      this.setData({
        dialogShow: true
    })
    }
  },
  /**
   * 微信登录
   */
  loginwx:function(e){

  },
  /**
   * 手机登录
   * @param {*} e 
   */
  phonelogin:function(e){
        wx.navigateTo({
          url: './register/register',
    })
  },
  bindGetUserInfo(e){
    this.setData({
      dialogShow:false
    })
    if(e.detail.userInfo){
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        nickName:e.detail.userInfo.nickName,
        avatarUrl:e.detail.userInfo.avatarUrl
      })
      let reqCode = {
        code:app.globalData.code,
        nickName:app.globalData.userInfo.nickName,
        avatarUrl:app.globalData.userInfo.avatarUrl,
        gender:app.globalData.userInfo.gender,
        province:app.globalData.userInfo.province,
        city:app.globalData.userInfo.city,
        country:app.globalData.userInfo.country,
      }
      myselfService.authorize(reqCode).then(s =>{
        console.log(s)
        wx.setStorageSync('token', s.access_token)
      },error =>{
        wx.showToast({
          title: '授权失败',
          icon: 'none',
          duration: 2000
        })
      })
    }
  },
  /**
   * 进入界面事件
   */
  onLoad(){
    if(app.globalData.userInfo){
      this.setData({
        nickName:app.globalData.userInfo.nickName,
        avatarUrl:app.globalData.userInfo.avatarUrl
      })
      console.log(app.globalData.userInfo)
    }else{
      this.setData({
        nickName:'未登录/注册'
      })
    }
  }
})